package com.ququ.syb.business.payment.controller;

import com.ququ.common.result.Result;
import com.ququ.common.result.ResultJson;
import com.ququ.syb.business.payment.pojo.PayResult;
import com.ququ.syb.business.payment.service.PayOrderService;
import com.ququ.syb.business.payment.service.PaymentBusinessService;
import com.ququ.syb.common.pojo.PayOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentBusinessController {
    @Autowired
    private PaymentBusinessService paymentBusinessService;


    @RequestMapping("/payorder/create")
    public Result<PayOrder> createPayOrder(@RequestParam String orderno,
                                           @RequestParam Long amount,
                                           @RequestParam Integer ordertype,
                                           @RequestParam String title,
                                           @RequestParam(required = false) String desc,
                                           @RequestParam(required = false) String returnurl,
                                           @RequestParam String callbackurl,
                                           @RequestParam(required = false) String withdrawcallbackurl,
                                           @RequestParam(required = false) String payaccountid,
                                           @RequestParam String payeeaccountid,
                                           @RequestParam(required = false) Long paycommission,
                                           @RequestParam(required = false) Long payeecommission,
                                           @RequestParam String appcode,
                                           @RequestParam String paychannelid,
                                           @RequestParam(required = false) Integer assureday,
                                           @RequestParam(required = false) Integer timeout,
                                           @RequestParam(required = false) String divideinfo){
        return paymentBusinessService.createPayOrder(orderno, amount, ordertype, title, desc, returnurl, callbackurl, withdrawcallbackurl, payaccountid, payeeaccountid, paycommission, payeecommission, appcode, paychannelid, assureday, timeout, divideinfo);
    }


    @RequestMapping("/payorder/pay")
    public Result<PayResult> paymentOrPaySms(@RequestParam String tradenum,
                                             @RequestParam(required = false) String name,
                                             @RequestParam(required = false) String bankcardno,
                                             @RequestParam(required = false) String mobile,
                                             @RequestParam(required = false) String idcardno,
                                             @RequestParam(required = false) String expirydate,
                                             @RequestParam(required = false) String cvv2,
                                             @RequestParam(required = false) String authcode,
                                             @RequestParam(required = false) String openid,
                                             @RequestParam(required = false) String buyerid,
                                             @RequestParam(required = false) String userip,
                                             @RequestParam(required = false) String extparams){
        return paymentBusinessService.paymentOrPaySms(tradenum, name, bankcardno, mobile, idcardno, expirydate, cvv2, authcode, openid, buyerid, userip, extparams);
    }


    @RequestMapping("/payorder/quick/confirm")
    public Result<PayResult> quickPaySmsConfirm(@RequestParam String tradenum,
                                                @RequestParam String idcode){
        return paymentBusinessService.quickPaySmsConfirm(tradenum, idcode);
    }


    @RequestMapping("/payorder/quick/resend")
    public Result<PayResult> quickPayResendSms(@RequestParam String tradenum){
        return paymentBusinessService.quickPayResendSms(tradenum);
    }


    @RequestMapping("/payorder/callback")
    public ResultJson paymentCallback(@RequestParam String tradenum,
                                      @RequestParam int status,
                                      @RequestParam(required = false) String failreason){
        try{
            paymentBusinessService.paymentCallback(tradenum, status, failreason);
            return new ResultJson();
        }catch (Exception e){
            return new ResultJson(-1,"回调处理异常"+e.getMessage());
        }
    }


    @RequestMapping("/payorder/refund")
    public ResultJson paymentRefund(@RequestParam String tradenum){
        boolean success = paymentBusinessService.paymentRefund(tradenum);
        if (success)
            return new ResultJson();
        else
            return new ResultJson(-1,"退款失败");
    }


    @RequestMapping("/payorder/assure/confirm")
    public ResultJson paymentAssureConfirm(@RequestParam String tradenum){
        boolean success = paymentBusinessService.paymentAssureConfirm(tradenum);
        if (success)
            return new ResultJson();
        else
            return new ResultJson(-1,"担保确认");
    }


}
